<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang='en'>
  <head>
    <title>KRPC::ProtobufUtils::Encoder</title>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
    <link href='../../../css/style.css' media='screen' rel='stylesheet' type='text/css'>
    <script type='text/javascript'>
      //<![CDATA[
        function popupCode(url) {
          window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
        }
        
        function toggleCode(id) {
          var code = document.getElementById(id)
        
          code.style.display = code.style.display != 'block' ? 'block' : 'none'
          return true
        }
        
        // Make codeblocks hidden by default
        document.writeln('<' + 'style type="text/css">.method .source pre { display: none }<\/style>')
      //]]>
    </script>
  </head>
  <body class='page'>
    <div class='class' id='wrapper'>
      <div class='header'>
        <h1 class='name'>
          <span class='type'>module</span>
          KRPC::ProtobufUtils::Encoder
        </h1>
        <ol class='paths'>
          <li>
            <a target="docwin" href="../../../files/lib/krpc/protobuf_utils_rb.html">lib/krpc/protobuf_utils.rb</a>
          </li>
        </ol>
      </div>
      <div id='content'>
        <div id='text'>
          <div id='description'></div>
          <div id='method-list'>
            <h2>Methods</h2>
            <h3>Public Class</h3>
            <ol>
              <li><a target="docwin" href="#method-c-encode">encode</a></li>
              <li><a target="docwin" href="#method-c-encode_bool">encode_bool</a></li>
              <li><a target="docwin" href="#method-c-encode_bytes">encode_bytes</a></li>
              <li><a target="docwin" href="#method-c-encode_double">encode_double</a></li>
              <li><a target="docwin" href="#method-c-encode_float">encode_float</a></li>
              <li><a target="docwin" href="#method-c-encode_nonnegative_varint">encode_nonnegative_varint</a></li>
              <li><a target="docwin" href="#method-c-encode_signed_varint">encode_signed_varint</a></li>
              <li><a target="docwin" href="#method-c-encode_string">encode_string</a></li>
              <li><a target="docwin" href="#method-c-encode_varint">encode_varint</a></li>
            </ol>
          </div>
          <div id='context'>
          </div>
          <div id='section'>
            <div id='aliases-list'>
              <h2>Public Class Aliases</h2>
              <div class='name-list'>
                <table summary='Public Class Aliases'>
                  <tr class='top-aligned-row context-row'>
                    <td class='context-item-name'>encode_int32</td>
                    <td>-&gt;</td>
                    <td class='context-item-value'><a target="docwin" href="#method-c-encode_signed_varint">encode_signed_varint</a></td>
                  </tr>
                  <tr class='top-aligned-row context-row'>
                    <td class='context-item-name'>encode_int64</td>
                    <td>-&gt;</td>
                    <td class='context-item-value'><a target="docwin" href="#method-c-encode_signed_varint">encode_signed_varint</a></td>
                  </tr>
                  <tr class='top-aligned-row context-row'>
                    <td class='context-item-name'>encode_uint32</td>
                    <td>-&gt;</td>
                    <td class='context-item-value'><a target="docwin" href="#method-c-encode_nonnegative_varint">encode_nonnegative_varint</a></td>
                  </tr>
                  <tr class='top-aligned-row context-row'>
                    <td class='context-item-name'>encode_uint64</td>
                    <td>-&gt;</td>
                    <td class='context-item-value'><a target="docwin" href="#method-c-encode_nonnegative_varint">encode_nonnegative_varint</a></td>
                  </tr>
                </table>
              </div>
            </div>
            <div id='methods'>
              <h2>Public Class methods</h2>
              <div class='method public-class' id='method-method-c-encode'>
                <a name='method-c-encode'></a>
                <div class='synopsis'>
                  <span class='name'>encode</span>
                  <span class='arguments'>(value, type)</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-c-encode-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-c-encode-source'><span class="ruby-comment"># File lib/krpc/protobuf_utils.rb, line 75</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">encode</span>(<span class="ruby-identifier">value</span>, <span class="ruby-identifier">type</span>)&#x000A;  <span class="ruby-identifier">meth_name</span> = <span class="ruby-string">&quot;encode_&quot;</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">type</span>&#x000A;  <span class="ruby-identifier">raise</span>(<span class="ruby-constant">RuntimeError</span>, <span class="ruby-node">&quot;Unsupported type #{type}&quot;</span>) <span class="ruby-keyword">unless</span> <span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">meth_name</span>)&#x000A;  <span class="ruby-identifier">send</span>(<span class="ruby-identifier">meth_name</span>, <span class="ruby-identifier">value</span>)&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-class' id='method-method-c-encode_bool'>
                <a name='method-c-encode_bool'></a>
                <div class='synopsis'>
                  <span class='name'>encode_bool</span>
                  <span class='arguments'>(value)</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-c-encode_bool-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-c-encode_bool-source'><span class="ruby-comment"># File lib/krpc/protobuf_utils.rb, line 115</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">encode_bool</span>(<span class="ruby-identifier">value</span>)&#x000A;  <span class="ruby-identifier">encode_varint</span>(<span class="ruby-identifier">value</span> <span class="ruby-operator">?</span> <span class="ruby-value">1</span> <span class="ruby-operator">:</span> <span class="ruby-value">0</span>)&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-class' id='method-method-c-encode_bytes'>
                <a name='method-c-encode_bytes'></a>
                <div class='synopsis'>
                  <span class='name'>encode_bytes</span>
                  <span class='arguments'>(value)</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-c-encode_bytes-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-c-encode_bytes-source'><span class="ruby-comment"># File lib/krpc/protobuf_utils.rb, line 122</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">encode_bytes</span>(<span class="ruby-identifier">value</span>)&#x000A;  <span class="ruby-identifier">size</span> = <span class="ruby-identifier">encode_varint</span>(<span class="ruby-identifier">value</span>.<span class="ruby-identifier">size</span>)&#x000A;  <span class="ruby-identifier">size</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">map</span>(<span class="ruby-operator">&amp;</span><span class="ruby-value">:chr</span>).<span class="ruby-identifier">join</span>.<span class="ruby-identifier">b</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-class' id='method-method-c-encode_double'>
                <a name='method-c-encode_double'></a>
                <div class='synopsis'>
                  <span class='name'>encode_double</span>
                  <span class='arguments'>(value)</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-c-encode_double-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-c-encode_double-source'><span class="ruby-comment"># File lib/krpc/protobuf_utils.rb, line 112</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">encode_double</span>(<span class="ruby-identifier">value</span>)&#x000A;  [<span class="ruby-identifier">value</span>].<span class="ruby-identifier">pack</span>(<span class="ruby-string">&#39;E&#39;</span>)&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-class' id='method-method-c-encode_float'>
                <a name='method-c-encode_float'></a>
                <div class='synopsis'>
                  <span class='name'>encode_float</span>
                  <span class='arguments'>(value)</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-c-encode_float-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-c-encode_float-source'><span class="ruby-comment"># File lib/krpc/protobuf_utils.rb, line 109</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">encode_float</span>(<span class="ruby-identifier">value</span>)&#x000A;  [<span class="ruby-identifier">value</span>].<span class="ruby-identifier">pack</span>(<span class="ruby-string">&#39;e&#39;</span>)&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-class' id='method-method-c-encode_nonnegative_varint'>
                <a name='method-c-encode_nonnegative_varint'></a>
                <div class='synopsis'>
                  <span class='name'>encode_nonnegative_varint</span>
                  <span class='arguments'>(value)</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-c-encode_nonnegative_varint-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-c-encode_nonnegative_varint-source'><span class="ruby-comment"># File lib/krpc/protobuf_utils.rb, line 99</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">encode_nonnegative_varint</span>(<span class="ruby-identifier">value</span>)&#x000A;  <span class="ruby-identifier">raise</span>(<span class="ruby-constant">RangeError</span>, <span class="ruby-node">&quot;Value must be non-negative, got #{value}&quot;</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">value</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">0</span>&#x000A;  <span class="ruby-identifier">encode_varint</span>(<span class="ruby-identifier">value</span>)&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-class' id='method-method-c-encode_signed_varint'>
                <a name='method-c-encode_signed_varint'></a>
                <div class='synopsis'>
                  <span class='name'>encode_signed_varint</span>
                  <span class='arguments'>(value)</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-c-encode_signed_varint-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-c-encode_signed_varint-source'><span class="ruby-comment"># File lib/krpc/protobuf_utils.rb, line 95</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">encode_signed_varint</span>(<span class="ruby-identifier">value</span>)&#x000A;  <span class="ruby-identifier">value</span> <span class="ruby-operator">+=</span> (<span class="ruby-value">1</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-value">64</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">value</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">0</span>&#x000A;  <span class="ruby-identifier">encode_varint</span>(<span class="ruby-identifier">value</span>)&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-class' id='method-method-c-encode_string'>
                <a name='method-c-encode_string'></a>
                <div class='synopsis'>
                  <span class='name'>encode_string</span>
                  <span class='arguments'>(value)</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-c-encode_string-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-c-encode_string-source'><span class="ruby-comment"># File lib/krpc/protobuf_utils.rb, line 118</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">encode_string</span>(<span class="ruby-identifier">value</span>)&#x000A;  <span class="ruby-identifier">size</span> = <span class="ruby-identifier">encode_varint</span>(<span class="ruby-identifier">value</span>.<span class="ruby-identifier">bytesize</span>)&#x000A;  <span class="ruby-identifier">size</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">bytes</span>.<span class="ruby-identifier">map</span>(<span class="ruby-operator">&amp;</span><span class="ruby-value">:chr</span>).<span class="ruby-identifier">join</span>.<span class="ruby-identifier">b</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-class' id='method-method-c-encode_varint'>
                <a name='method-c-encode_varint'></a>
                <div class='synopsis'>
                  <span class='name'>encode_varint</span>
                  <span class='arguments'>(value)</span>
                </div>
                <div class='description'>
                  
                  <p>based on: <a
                  href="http://www.rubydoc.info/gems/ruby-protocol-buffers/1.0.1/ProtocolBuffers/Varint#decode-class_method">www.rubydoc.info/gems/ruby-protocol-buffers/1.0.1/ProtocolBuffers/Varint#decode-class_method</a>
                  &amp; <a
                  href="https://github.com/google/protobuf/blob/master/python/google/protobuf/internal/encoder.py#L390">github.com/google/protobuf/blob/master/python/google/protobuf/internal/encoder.py#L390</a></p>
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-c-encode_varint-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-c-encode_varint-source'><span class="ruby-comment"># File lib/krpc/protobuf_utils.rb, line 82</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">encode_varint</span>(<span class="ruby-identifier">value</span>)&#x000A;  <span class="ruby-keyword">return</span> [<span class="ruby-identifier">value</span>].<span class="ruby-identifier">pack</span>(<span class="ruby-string">&#39;C&#39;</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">value</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">0b1000_0000</span>&#x000A;  <span class="ruby-identifier">result</span> = <span class="ruby-string">&quot;&quot;</span>&#x000A;  <span class="ruby-identifier">loop</span> <span class="ruby-keyword">do</span>&#x000A;    <span class="ruby-identifier">byte</span> = <span class="ruby-identifier">value</span> <span class="ruby-operator">&amp;</span> <span class="ruby-value">0b0111_1111</span>&#x000A;    <span class="ruby-identifier">value</span> <span class="ruby-operator">&gt;&gt;=</span> <span class="ruby-value">7</span>&#x000A;    <span class="ruby-keyword">if</span> <span class="ruby-identifier">value</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>&#x000A;      <span class="ruby-keyword">return</span> <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">byte</span>.<span class="ruby-identifier">chr</span>&#x000A;    <span class="ruby-keyword">else</span>&#x000A;      <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> (<span class="ruby-identifier">byte</span> <span class="ruby-operator">|</span> <span class="ruby-value">0b1000_0000</span>).<span class="ruby-identifier">chr</span>&#x000A;    <span class="ruby-keyword">end</span>&#x000A;  <span class="ruby-keyword">end</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div id='footer-push'></div>
    </div>
    <div id='footer'>
      <a target="docwin" href="http://github.com/mislav/hanna/tree/master"><strong>Hanna</strong> RDoc template</a>
    </div>
  </body>
</html>
